import cn.hutool.core.collection.CollUtil
import cn.hutool.core.date.DateUtil
import cn.hutool.core.util.NumberUtil

import java.math.RoundingMode

def contractInfo = ["agreementNo"  : "5431",
                    "activityId"   : "1932",
                    "agreementType": "SINGLE_SELLER_GOODS",
                    "startTime"    : 1680278400000,
                    "endTime"      : 1704038399999,
                    "sendType"     : 1,
                    "brandType"    : 2,
                    "accountType"  : 2,
                    "contractNo"   : "CDKG-20230626-056",
                    "targetInfo"   : ["gearInfos": [["settleGmvTarget"           : 1500000000,
                                                     "beFxSettleGmvTarget"       : 5817990000,
                                                     "businessCostTarget"        : 44100000,
                                                     "gmvTrRebateWf"             : 2500,
                                                     "beFxGmvTrRebateWf"         : 1000,
                                                     "businessCostRebateWf"      : 200,
                                                     "businessCostYearlyRebateWf": 3000],
                                                    ["settleGmvTarget"           : 2034020000,
                                                     "beFxSettleGmvTarget"       : 6323900000,
                                                     "businessCostTarget"        : 47930000,
                                                     "gmvTrRebateWf"             : 6000,
                                                     "beFxGmvTrRebateWf"         : 1500,
                                                     "businessCostRebateWf"      : 200,
                                                     "businessCostYearlyRebateWf": 3500],
                                                    ["settleGmvTarget"           : 2139980000,
                                                     "beFxSettleGmvTarget"       : 6829820000,
                                                     "businessCostTarget"        : 51760000,
                                                     "gmvTrRebateWf"             : 8250,
                                                     "beFxGmvTrRebateWf"         : 2000,
                                                     "businessCostRebateWf"      : 200,
                                                     "businessCostYearlyRebateWf": 4000]]],
                    "createTime"   : 1689325524142,
                    "updateTime"   : 1689325524142,
                    "targetInfoStr": "{\"gearInfoList\":[{\"businessCostYearlyRebateWf\":3000,\"businessCostTarget\":44100000,\"gmvTrRebateWf\":2500,\"beFxGmvTrRebateWf\":1000,\"settleGmvTarget\":1500000000,\"businessCostRebateWf\":200,\"beFxSettleGmvTarget\":5817990000},{\"businessCostYearlyRebateWf\":3500,\"businessCostTarget\":47930000,\"gmvTrRebateWf\":6000,\"beFxGmvTrRebateWf\":1500,\"settleGmvTarget\":2034020000,\"businessCostRebateWf\":200,\"beFxSettleGmvTarget\":6323900000},{\"businessCostYearlyRebateWf\":4000,\"businessCostTarget\":51760000,\"gmvTrRebateWf\":8250,\"beFxGmvTrRebateWf\":2000,\"settleGmvTarget\":2139980000,\"businessCostRebateWf\":200,\"beFxSettleGmvTarget\":6829820000}]}"]
def settlementEnd = "2023-06-30 00:00:00"
def brandStats = [["pDate"           : "20230630",
                   "sellerId"        : 1319540256,
                   "ksBrandId"       : -1,
                   "outerBrandLevel" : null,
                   "selfSettleGmvCtd": 3867097,
                   "contractRuleId"  : 5431,
                   "ksBrandName"     : null],
                  ["pDate"           : "20230630",
                   "sellerId"        : 1319540256,
                   "ksBrandId"       : 27202,
                   "outerBrandLevel" : "S2",
                   "selfSettleGmvCtd": 255181258,
                   "contractRuleId"  : 5431,
                   "ksBrandName"     : "森马"],
                  ["pDate"           : "20230630",
                   "sellerId"        : 1842042480,
                   "ksBrandId"       : -1,
                   "outerBrandLevel" : null,
                   "selfSettleGmvCtd": 0,
                   "contractRuleId"  : 5431,
                   "ksBrandName"     : null],
                  ["pDate"           : "20230630",
                   "sellerId"        : 1842042480,
                   "ksBrandId"       : 18439,
                   "outerBrandLevel" : "S2",
                   "selfSettleGmvCtd": 23669443,
                   "contractRuleId"  : 5431,
                   "ksBrandName"     : "迷你巴拉巴拉"],
                  ["pDate"           : "20230630",
                   "sellerId"        : 2123138121,
                   "ksBrandId"       : -1,
                   "outerBrandLevel" : null,
                   "selfSettleGmvCtd": 0,
                   "contractRuleId"  : 5431,
                   "ksBrandName"     : null],
                  ["pDate"           : "20230630",
                   "sellerId"        : 2123138121,
                   "ksBrandId"       : 3664,
                   "outerBrandLevel" : "S1",
                   "selfSettleGmvCtd": 124415201,
                   "contractRuleId"  : 5431,
                   "ksBrandName"     : "巴拉巴拉"],
                  ["pDate"           : "20230630",
                   "sellerId"        : 2377201810,
                   "ksBrandId"       : -1,
                   "outerBrandLevel" : null,
                   "selfSettleGmvCtd": 0,
                   "contractRuleId"  : 5431,
                   "ksBrandName"     : null],
                  ["pDate"           : "20230630",
                   "sellerId"        : 2377201810,
                   "ksBrandId"       : 3664,
                   "outerBrandLevel" : "S1",
                   "selfSettleGmvCtd": 68062608,
                   "contractRuleId"  : 5431,
                   "ksBrandName"     : "巴拉巴拉"],
                  ["pDate"           : "20230630",
                   "sellerId"        : 2382774937,
                   "ksBrandId"       : -1,
                   "outerBrandLevel" : null,
                   "selfSettleGmvCtd": 1664916,
                   "contractRuleId"  : 5431,
                   "ksBrandName"     : null],
                  ["pDate"           : "20230630",
                   "sellerId"        : 2382774937,
                   "ksBrandId"       : 27202,
                   "outerBrandLevel" : "S2",
                   "selfSettleGmvCtd": 11584917,
                   "contractRuleId"  : 5431,
                   "ksBrandName"     : "森马"]]
def baseStats = [["sellerId"                                       : 1319540256,
                  "settleGmvLastYear"                              : null,
                  "disSettleGmvLastYear"                           : null,
                  "adJkTotalCostLastYear"                          : null,
                  "totalCostLastYear"                              : null,
                  "settleTrLastYear"                               : 0.04998162,
                  "disSettleTrLastYear"                            : 0.05041776,
                  "settleGmvYoyLastYear"                           : null,
                  "disSettleGmvYoyLastYear"                        : null,
                  "actyDaysBeforeLastYear"                         : 275,
                  "rslrActyDaysBeforeLastYear"                     : 275,
                  "adJfTotalCostLastYear"                          : null,
                  "settleGmvBeforeLastYear"                        : null,
                  "disSettleGmvBeforeLastYear"                     : null,
                  "fromSellerPlatfromSettleGmvLastYear"            : null,
                  "disFromSellerPlatfromSettleGmvLastYear"         : null,
                  "sellerIndustryType"                             : "行业运营",
                  "sellerIndustryGroup"                            : "综合服饰",
                  "userName"                                       : null,
                  "staffEmail"                                     : "wudi22",
                  "saleOwner"                                      : "liangzhixin",
                  "isXn"                                           : 0,
                  "isSingle"                                       : 0,
                  "isS12"                                          : 1,
                  "departNameLevel1"                               : "CRM商业化",
                  "departNameLevel2"                               : "服饰业务中心",
                  "departNameLevel3"                               : "综合服饰业务组",
                  "adJkTotalCostYtd"                               : 6991842.8,
                  "adJfTotalCostYtd"                               : 6991842.8,
                  "adJkTotalCost7d"                                : 367083.60000000003,
                  "adJfTotalCost7d"                                : 367083.60000000003,
                  "adJkTotalCost30d"                               : 3267205.3000000003,
                  "adJfTotalCost30d"                               : 3267205.3000000003,
                  "settleGmvYtd"                                   : null,
                  "settleGmv30d"                                   : null,
                  "fromSellerPlatfromSettleAmtYtd"                 : 12758896,
                  "fromSellerPlatfromSettleAmt30d"                 : 5550037,
                  "disSettleGmvYtd"                                : null,
                  "disSettleGmv30d"                                : null,
                  "disFromSellerPlatfromSettleAmtYtd"              : 674979,
                  "disFromSellerPlatfromSettleAmt30d"              : 0,
                  "rslrActyDaysLastYear"                           : 275,
                  "actyDaysLastYear"                               : 254,
                  "totalCostYtd"                                   : null,
                  "totalCost7d"                                    : null,
                  "totalCost30d"                                   : null,
                  "settleTrYtd"                                    : 0.04945031,
                  "disSettleTrYtd"                                 : 0.04709662,
                  "contractRuleId"                                 : 5431,
                  "companyName"                                    : "浙江森马电子商务有限公司",
                  "ruleTemplate"                                   : "single_seller_goods",
                  "accountType"                                    : 2,
                  "brandType"                                      : 2,
                  "executeType"                                    : 1,
                  "auditFlowId"                                    : "CDKG-20230626-056",
                  "status"                                         : 31,
                  "l1OrgName"                                      : "行业运营",
                  "l2OrgName"                                      : "女装",
                  "startTime"                                      : 1680278400000,
                  "endTime"                                        : 1704038399999,
                  "creator"                                        : "cengxiankai",
                  "lowSettleAmtTarget"                             : 1500000000,
                  "midSettleAmtTarget"                             : 2034020000,
                  "highSettleAmtTarget"                            : 2139980000,
                  "lowDisSettleAmtTarget"                          : 5817990000,
                  "midDisSettleAmtTarget"                          : 6323900000,
                  "highDisSettleAmtTarget"                         : 6829820000,
                  "lowAdCostTarget"                                : null,
                  "midAdCostTarget"                                : null,
                  "highAdCostTarget"                               : null,
                  "lowAdCostRebate"                                : null,
                  "midAdCostRebate"                                : null,
                  "highAdCostRebate"                               : null,
                  "lowSettleAmtRebate"                             : "*结算TR*25%",
                  "midSettleAmtRebate"                             : "*结算TR*60%",
                  "highSettleAmtRebate"                            : "*结算TR*82.5%",
                  "lowDisSettleAmtRebate"                          : "*被分销结算TR*10%",
                  "midDisSettleAmtRebate"                          : "*被分销结算TR*15%",
                  "highDisSettleAmtRebate"                         : "*被分销结算TR*20%",
                  "securityDeposit"                                : 3659988,
                  "securityDepositStatus"                          : 0,
                  "frameDays"                                      : 274,
                  "frameCompDays"                                  : 90,
                  "isMainSlr"                                      : 1,
                  "adJkTotalCostCtd"                               : 6991842.8,
                  "adJfTotalCostCtd"                               : 6991842.8,
                  "settleGmvCtd"                                   : null,
                  "fromSellerPlatfromSettleAmtCtd"                 : 12758896,
                  "disSettleGmvCtd"                                : null,
                  "disFromSellerPlatfromSettleAmtCtd"              : 674979,
                  "totalCostCtd"                                   : null,
                  "settleTrCtd"                                    : 0.04945031,
                  "disSettleTrCtd"                                 : 0.04709662,
                  "top1BrandPayGmvCtd"                             : null,
                  "payGmvCtd"                                      : null,
                  "isVideoSlr"                                     : 0,
                  "isS12Slr"                                       : 0,
                  "frameDisSettleGmvCtd"                           : null,
                  "adActyDaysLastYear"                             : null,
                  "frameDisFromSellerPlatfromSettleAmtCtd"         : 674979,
                  "frameDisSettleTrCtd"                            : 0.04709662,
                  "settleGmvLastMonthCtd"                          : null,
                  "fromSellerPlatfromLastMonthCtd"                 : 7208859,
                  "disSettleGmvLastMonthCtd"                       : null,
                  "disFromSellerPlatfromSettleAmtLastMonthCtd"     : 674979,
                  "frameDisSettleGmvLastMonthCtd"                  : null,
                  "frameDisFromSellerPlatfromSettleAmtLastMonthCtd": 674979,
                  "settleTrLastMonthCtd"                           : 0.04959151,
                  "disSettleTrLastMonthCtd"                        : 0.04709662,
                  "frameDisSettleTrLastMonthCtd"                   : 0.04709662,
                  "isContractRisk"                                 : 0,
                  "pDate"                                          : "20230630"],
                 ["sellerId"                                       : 1842042480,
                  "settleGmvLastYear"                              : null,
                  "disSettleGmvLastYear"                           : null,
                  "adJkTotalCostLastYear"                          : null,
                  "totalCostLastYear"                              : null,
                  "settleTrLastYear"                               : 0.04991359,
                  "disSettleTrLastYear"                            : 0.04997037,
                  "settleGmvYoyLastYear"                           : null,
                  "disSettleGmvYoyLastYear"                        : null,
                  "actyDaysBeforeLastYear"                         : 269,
                  "rslrActyDaysBeforeLastYear"                     : 274,
                  "adJfTotalCostLastYear"                          : null,
                  "settleGmvBeforeLastYear"                        : null,
                  "disSettleGmvBeforeLastYear"                     : null,
                  "fromSellerPlatfromSettleGmvLastYear"            : null,
                  "disFromSellerPlatfromSettleGmvLastYear"         : null,
                  "sellerIndustryType"                             : "行业运营",
                  "sellerIndustryGroup"                            : "母婴图书",
                  "userName"                                       : null,
                  "staffEmail"                                     : "caiqi",
                  "saleOwner"                                      : null,
                  "isXn"                                           : 0,
                  "isSingle"                                       : 0,
                  "isS12"                                          : 1,
                  "departNameLevel1"                               : null,
                  "departNameLevel2"                               : null,
                  "departNameLevel3"                               : null,
                  "adJkTotalCostYtd"                               : 0.0,
                  "adJfTotalCostYtd"                               : 0.0,
                  "adJkTotalCost7d"                                : 0.0,
                  "adJfTotalCost7d"                                : 0.0,
                  "adJkTotalCost30d"                               : 0.0,
                  "adJfTotalCost30d"                               : 0.0,
                  "settleGmvYtd"                                   : null,
                  "settleGmv30d"                                   : null,
                  "fromSellerPlatfromSettleAmtYtd"                 : 1184399,
                  "fromSellerPlatfromSettleAmt30d"                 : 265782,
                  "disSettleGmvYtd"                                : null,
                  "disSettleGmv30d"                                : null,
                  "disFromSellerPlatfromSettleAmtYtd"              : 10917735,
                  "disFromSellerPlatfromSettleAmt30d"              : 2002020,
                  "rslrActyDaysLastYear"                           : 275,
                  "actyDaysLastYear"                               : 274,
                  "totalCostYtd"                                   : null,
                  "totalCost7d"                                    : null,
                  "totalCost30d"                                   : null,
                  "settleTrYtd"                                    : 0.05003737,
                  "disSettleTrYtd"                                 : 0.04996373,
                  "contractRuleId"                                 : 5431,
                  "companyName"                                    : "浙江森马电子商务有限公司",
                  "ruleTemplate"                                   : "single_seller_goods",
                  "accountType"                                    : 2,
                  "brandType"                                      : 2,
                  "executeType"                                    : 1,
                  "auditFlowId"                                    : "CDKG-20230626-056",
                  "status"                                         : 31,
                  "l1OrgName"                                      : "行业运营",
                  "l2OrgName"                                      : "女装",
                  "startTime"                                      : 1680278400000,
                  "endTime"                                        : 1704038399999,
                  "creator"                                        : "cengxiankai",
                  "lowSettleAmtTarget"                             : 1500000000,
                  "midSettleAmtTarget"                             : 2034020000,
                  "highSettleAmtTarget"                            : 2139980000,
                  "lowDisSettleAmtTarget"                          : 5817990000,
                  "midDisSettleAmtTarget"                          : 6323900000,
                  "highDisSettleAmtTarget"                         : 6829820000,
                  "lowAdCostTarget"                                : null,
                  "midAdCostTarget"                                : null,
                  "highAdCostTarget"                               : null,
                  "lowAdCostRebate"                                : null,
                  "midAdCostRebate"                                : null,
                  "highAdCostRebate"                               : null,
                  "lowSettleAmtRebate"                             : "*结算TR*25%",
                  "midSettleAmtRebate"                             : "*结算TR*60%",
                  "highSettleAmtRebate"                            : "*结算TR*82.5%",
                  "lowDisSettleAmtRebate"                          : "*被分销结算TR*10%",
                  "midDisSettleAmtRebate"                          : "*被分销结算TR*15%",
                  "highDisSettleAmtRebate"                         : "*被分销结算TR*20%",
                  "securityDeposit"                                : 3659988,
                  "securityDepositStatus"                          : 0,
                  "frameDays"                                      : 274,
                  "frameCompDays"                                  : 90,
                  "isMainSlr"                                      : 0,
                  "adJkTotalCostCtd"                               : 0.0,
                  "adJfTotalCostCtd"                               : 0.0,
                  "settleGmvCtd"                                   : null,
                  "fromSellerPlatfromSettleAmtCtd"                 : 1184399,
                  "disSettleGmvCtd"                                : null,
                  "disFromSellerPlatfromSettleAmtCtd"              : 10917735,
                  "totalCostCtd"                                   : null,
                  "settleTrCtd"                                    : 0.05003737,
                  "disSettleTrCtd"                                 : 0.04996373,
                  "top1BrandPayGmvCtd"                             : null,
                  "payGmvCtd"                                      : null,
                  "isVideoSlr"                                     : 0,
                  "isS12Slr"                                       : 0,
                  "frameDisSettleGmvCtd"                           : null,
                  "adActyDaysLastYear"                             : null,
                  "frameDisFromSellerPlatfromSettleAmtCtd"         : 10917735,
                  "frameDisSettleTrCtd"                            : 0.04996373,
                  "settleGmvLastMonthCtd"                          : null,
                  "fromSellerPlatfromLastMonthCtd"                 : 918617,
                  "disSettleGmvLastMonthCtd"                       : null,
                  "disFromSellerPlatfromSettleAmtLastMonthCtd"     : 8915715,
                  "frameDisSettleGmvLastMonthCtd"                  : null,
                  "frameDisFromSellerPlatfromSettleAmtLastMonthCtd": 8915715,
                  "settleTrLastMonthCtd"                           : 0.05003399,
                  "disSettleTrLastMonthCtd"                        : 0.04998549,
                  "frameDisSettleTrLastMonthCtd"                   : 0.04998549,
                  "isContractRisk"                                 : 0,
                  "pDate"                                          : "20230630"],
                 ["sellerId"                                       : 2123138121,
                  "settleGmvLastYear"                              : null,
                  "disSettleGmvLastYear"                           : null,
                  "adJkTotalCostLastYear"                          : null,
                  "totalCostLastYear"                              : null,
                  "settleTrLastYear"                               : 0.04992226,
                  "disSettleTrLastYear"                            : 0.04997371,
                  "settleGmvYoyLastYear"                           : null,
                  "disSettleGmvYoyLastYear"                        : null,
                  "actyDaysBeforeLastYear"                         : 0,
                  "rslrActyDaysBeforeLastYear"                     : 127,
                  "adJfTotalCostLastYear"                          : null,
                  "settleGmvBeforeLastYear"                        : null,
                  "disSettleGmvBeforeLastYear"                     : null,
                  "fromSellerPlatfromSettleGmvLastYear"            : null,
                  "disFromSellerPlatfromSettleGmvLastYear"         : null,
                  "sellerIndustryType"                             : "行业运营",
                  "sellerIndustryGroup"                            : "母婴图书",
                  "userName"                                       : null,
                  "staffEmail"                                     : "likele",
                  "saleOwner"                                      : null,
                  "isXn"                                           : 0,
                  "isSingle"                                       : 0,
                  "isS12"                                          : 1,
                  "departNameLevel1"                               : null,
                  "departNameLevel2"                               : null,
                  "departNameLevel3"                               : null,
                  "adJkTotalCostYtd"                               : 0.0,
                  "adJfTotalCostYtd"                               : 0.0,
                  "adJkTotalCost7d"                                : 0.0,
                  "adJfTotalCost7d"                                : 0.0,
                  "adJkTotalCost30d"                               : 0.0,
                  "adJfTotalCost30d"                               : 0.0,
                  "settleGmvYtd"                                   : null,
                  "settleGmv30d"                                   : null,
                  "fromSellerPlatfromSettleAmtYtd"                 : 6148493,
                  "fromSellerPlatfromSettleAmt30d"                 : 1491713,
                  "disSettleGmvYtd"                                : null,
                  "disSettleGmv30d"                                : null,
                  "disFromSellerPlatfromSettleAmtYtd"              : 33174724,
                  "disFromSellerPlatfromSettleAmt30d"              : 6942736,
                  "rslrActyDaysLastYear"                           : 275,
                  "actyDaysLastYear"                               : 246,
                  "totalCostYtd"                                   : null,
                  "totalCost7d"                                    : null,
                  "totalCost30d"                                   : null,
                  "settleTrYtd"                                    : 0.04969334,
                  "disSettleTrYtd"                                 : 0.04987685,
                  "contractRuleId"                                 : 5431,
                  "companyName"                                    : "浙江森马电子商务有限公司",
                  "ruleTemplate"                                   : "single_seller_goods",
                  "accountType"                                    : 2,
                  "brandType"                                      : 2,
                  "executeType"                                    : 1,
                  "auditFlowId"                                    : "CDKG-20230626-056",
                  "status"                                         : 31,
                  "l1OrgName"                                      : "行业运营",
                  "l2OrgName"                                      : "女装",
                  "startTime"                                      : 1680278400000,
                  "endTime"                                        : 1704038399999,
                  "creator"                                        : "cengxiankai",
                  "lowSettleAmtTarget"                             : 1500000000,
                  "midSettleAmtTarget"                             : 2034020000,
                  "highSettleAmtTarget"                            : 2139980000,
                  "lowDisSettleAmtTarget"                          : 5817990000,
                  "midDisSettleAmtTarget"                          : 6323900000,
                  "highDisSettleAmtTarget"                         : 6829820000,
                  "lowAdCostTarget"                                : null,
                  "midAdCostTarget"                                : null,
                  "highAdCostTarget"                               : null,
                  "lowAdCostRebate"                                : null,
                  "midAdCostRebate"                                : null,
                  "highAdCostRebate"                               : null,
                  "lowSettleAmtRebate"                             : "*结算TR*25%",
                  "midSettleAmtRebate"                             : "*结算TR*60%",
                  "highSettleAmtRebate"                            : "*结算TR*82.5%",
                  "lowDisSettleAmtRebate"                          : "*被分销结算TR*10%",
                  "midDisSettleAmtRebate"                          : "*被分销结算TR*15%",
                  "highDisSettleAmtRebate"                         : "*被分销结算TR*20%",
                  "securityDeposit"                                : 3659988,
                  "securityDepositStatus"                          : 0,
                  "frameDays"                                      : 274,
                  "frameCompDays"                                  : 90,
                  "isMainSlr"                                      : 0,
                  "adJkTotalCostCtd"                               : 0.0,
                  "adJfTotalCostCtd"                               : 0.0,
                  "settleGmvCtd"                                   : null,
                  "fromSellerPlatfromSettleAmtCtd"                 : 6148493,
                  "disSettleGmvCtd"                                : null,
                  "disFromSellerPlatfromSettleAmtCtd"              : 33174724,
                  "totalCostCtd"                                   : null,
                  "settleTrCtd"                                    : 0.04969334,
                  "disSettleTrCtd"                                 : 0.04987685,
                  "top1BrandPayGmvCtd"                             : null,
                  "payGmvCtd"                                      : null,
                  "isVideoSlr"                                     : 0,
                  "isS12Slr"                                       : 0,
                  "frameDisSettleGmvCtd"                           : null,
                  "adActyDaysLastYear"                             : null,
                  "frameDisFromSellerPlatfromSettleAmtCtd"         : 33174724,
                  "frameDisSettleTrCtd"                            : 0.04987685,
                  "settleGmvLastMonthCtd"                          : null,
                  "fromSellerPlatfromLastMonthCtd"                 : 4656780,
                  "disSettleGmvLastMonthCtd"                       : null,
                  "disFromSellerPlatfromSettleAmtLastMonthCtd"     : 26231988,
                  "frameDisSettleGmvLastMonthCtd"                  : null,
                  "frameDisFromSellerPlatfromSettleAmtLastMonthCtd": 26231988,
                  "settleTrLastMonthCtd"                           : 0.04967896,
                  "disSettleTrLastMonthCtd"                        : 0.04987237,
                  "frameDisSettleTrLastMonthCtd"                   : 0.04987237,
                  "isContractRisk"                                 : 0,
                  "pDate"                                          : "20230630"],
                 ["sellerId"                                       : 2377201810,
                  "settleGmvLastYear"                              : null,
                  "disSettleGmvLastYear"                           : null,
                  "adJkTotalCostLastYear"                          : null,
                  "totalCostLastYear"                              : null,
                  "settleTrLastYear"                               : 0.04995828,
                  "disSettleTrLastYear"                            : 0.05000033,
                  "settleGmvYoyLastYear"                           : null,
                  "disSettleGmvYoyLastYear"                        : null,
                  "actyDaysBeforeLastYear"                         : 178,
                  "rslrActyDaysBeforeLastYear"                     : 179,
                  "adJfTotalCostLastYear"                          : null,
                  "settleGmvBeforeLastYear"                        : null,
                  "disSettleGmvBeforeLastYear"                     : null,
                  "fromSellerPlatfromSettleGmvLastYear"            : null,
                  "disFromSellerPlatfromSettleGmvLastYear"         : null,
                  "sellerIndustryType"                             : "行业运营",
                  "sellerIndustryGroup"                            : "母婴图书",
                  "userName"                                       : null,
                  "staffEmail"                                     : "likele",
                  "saleOwner"                                      : null,
                  "isXn"                                           : 0,
                  "isSingle"                                       : 0,
                  "isS12"                                          : 1,
                  "departNameLevel1"                               : null,
                  "departNameLevel2"                               : null,
                  "departNameLevel3"                               : null,
                  "adJkTotalCostYtd"                               : 0.0,
                  "adJfTotalCostYtd"                               : 0.0,
                  "adJkTotalCost7d"                                : 0.0,
                  "adJfTotalCost7d"                                : 0.0,
                  "adJkTotalCost30d"                               : 0.0,
                  "adJfTotalCost30d"                               : 0.0,
                  "settleGmvYtd"                                   : null,
                  "settleGmv30d"                                   : null,
                  "fromSellerPlatfromSettleAmtYtd"                 : 3300473,
                  "fromSellerPlatfromSettleAmt30d"                 : 784776,
                  "disSettleGmvYtd"                                : null,
                  "disSettleGmv30d"                                : null,
                  "disFromSellerPlatfromSettleAmtYtd"              : 21558139,
                  "disFromSellerPlatfromSettleAmt30d"              : 4447461,
                  "rslrActyDaysLastYear"                           : 275,
                  "actyDaysLastYear"                               : 275,
                  "totalCostYtd"                                   : null,
                  "totalCost7d"                                    : null,
                  "totalCost30d"                                   : null,
                  "settleTrYtd"                                    : 0.04894085,
                  "disSettleTrYtd"                                 : 0.04915395,
                  "contractRuleId"                                 : 5431,
                  "companyName"                                    : "浙江森马电子商务有限公司",
                  "ruleTemplate"                                   : "single_seller_goods",
                  "accountType"                                    : 2,
                  "brandType"                                      : 2,
                  "executeType"                                    : 1,
                  "auditFlowId"                                    : "CDKG-20230626-056",
                  "status"                                         : 31,
                  "l1OrgName"                                      : "行业运营",
                  "l2OrgName"                                      : "女装",
                  "startTime"                                      : 1680278400000,
                  "endTime"                                        : 1704038399999,
                  "creator"                                        : "cengxiankai",
                  "lowSettleAmtTarget"                             : 1500000000,
                  "midSettleAmtTarget"                             : 2034020000,
                  "highSettleAmtTarget"                            : 2139980000,
                  "lowDisSettleAmtTarget"                          : 5817990000,
                  "midDisSettleAmtTarget"                          : 6323900000,
                  "highDisSettleAmtTarget"                         : 6829820000,
                  "lowAdCostTarget"                                : null,
                  "midAdCostTarget"                                : null,
                  "highAdCostTarget"                               : null,
                  "lowAdCostRebate"                                : null,
                  "midAdCostRebate"                                : null,
                  "highAdCostRebate"                               : null,
                  "lowSettleAmtRebate"                             : "*结算TR*25%",
                  "midSettleAmtRebate"                             : "*结算TR*60%",
                  "highSettleAmtRebate"                            : "*结算TR*82.5%",
                  "lowDisSettleAmtRebate"                          : "*被分销结算TR*10%",
                  "midDisSettleAmtRebate"                          : "*被分销结算TR*15%",
                  "highDisSettleAmtRebate"                         : "*被分销结算TR*20%",
                  "securityDeposit"                                : 3659988,
                  "securityDepositStatus"                          : 0,
                  "frameDays"                                      : 274,
                  "frameCompDays"                                  : 90,
                  "isMainSlr"                                      : 0,
                  "adJkTotalCostCtd"                               : 0.0,
                  "adJfTotalCostCtd"                               : 0.0,
                  "settleGmvCtd"                                   : null,
                  "fromSellerPlatfromSettleAmtCtd"                 : 3300473,
                  "disSettleGmvCtd"                                : null,
                  "disFromSellerPlatfromSettleAmtCtd"              : 21558139,
                  "totalCostCtd"                                   : null,
                  "settleTrCtd"                                    : 0.04894085,
                  "disSettleTrCtd"                                 : 0.04915395,
                  "top1BrandPayGmvCtd"                             : null,
                  "payGmvCtd"                                      : null,
                  "isVideoSlr"                                     : 0,
                  "isS12Slr"                                       : 0,
                  "frameDisSettleGmvCtd"                           : null,
                  "adActyDaysLastYear"                             : null,
                  "frameDisFromSellerPlatfromSettleAmtCtd"         : 21558139,
                  "frameDisSettleTrCtd"                            : 0.04915395,
                  "settleGmvLastMonthCtd"                          : null,
                  "fromSellerPlatfromLastMonthCtd"                 : 2515697,
                  "disSettleGmvLastMonthCtd"                       : null,
                  "disFromSellerPlatfromSettleAmtLastMonthCtd"     : 17110678,
                  "frameDisSettleGmvLastMonthCtd"                  : null,
                  "frameDisFromSellerPlatfromSettleAmtLastMonthCtd": 17110678,
                  "settleTrLastMonthCtd"                           : 0.04896246,
                  "disSettleTrLastMonthCtd"                        : 0.04906851,
                  "frameDisSettleTrLastMonthCtd"                   : 0.04906851,
                  "isContractRisk"                                 : 0,
                  "pDate"                                          : "20230630"],
                 ["sellerId"                                       : 2382774937,
                  "settleGmvLastYear"                              : null,
                  "disSettleGmvLastYear"                           : null,
                  "adJkTotalCostLastYear"                          : null,
                  "totalCostLastYear"                              : null,
                  "settleTrLastYear"                               : 0.04969122,
                  "disSettleTrLastYear"                            : 0.05002344,
                  "settleGmvYoyLastYear"                           : null,
                  "disSettleGmvYoyLastYear"                        : null,
                  "actyDaysBeforeLastYear"                         : 169,
                  "rslrActyDaysBeforeLastYear"                     : 168,
                  "adJfTotalCostLastYear"                          : null,
                  "settleGmvBeforeLastYear"                        : null,
                  "disSettleGmvBeforeLastYear"                     : null,
                  "fromSellerPlatfromSettleGmvLastYear"            : null,
                  "disFromSellerPlatfromSettleGmvLastYear"         : null,
                  "sellerIndustryType"                             : "行业运营",
                  "sellerIndustryGroup"                            : "综合服饰",
                  "userName"                                       : null,
                  "staffEmail"                                     : "zhanglingyu",
                  "saleOwner"                                      : null,
                  "isXn"                                           : 1,
                  "isSingle"                                       : 0,
                  "isS12"                                          : 1,
                  "departNameLevel1"                               : null,
                  "departNameLevel2"                               : null,
                  "departNameLevel3"                               : null,
                  "adJkTotalCostYtd"                               : 0.0,
                  "adJfTotalCostYtd"                               : 0.0,
                  "adJkTotalCost7d"                                : 0.0,
                  "adJfTotalCost7d"                                : 0.0,
                  "adJkTotalCost30d"                               : 0.0,
                  "adJfTotalCost30d"                               : 0.0,
                  "settleGmvYtd"                                   : null,
                  "settleGmv30d"                                   : null,
                  "fromSellerPlatfromSettleAmtYtd"                 : 660713,
                  "fromSellerPlatfromSettleAmt30d"                 : 432573,
                  "disSettleGmvYtd"                                : null,
                  "disSettleGmv30d"                                : null,
                  "disFromSellerPlatfromSettleAmtYtd"              : 5669812,
                  "disFromSellerPlatfromSettleAmt30d"              : 4217429,
                  "rslrActyDaysLastYear"                           : 275,
                  "actyDaysLastYear"                               : 275,
                  "totalCostYtd"                                   : null,
                  "totalCost7d"                                    : null,
                  "totalCost30d"                                   : null,
                  "settleTrYtd"                                    : 0.0498208,
                  "disSettleTrYtd"                                 : 0.04992197,
                  "contractRuleId"                                 : 5431,
                  "companyName"                                    : "浙江森马电子商务有限公司",
                  "ruleTemplate"                                   : "single_seller_goods",
                  "accountType"                                    : 2,
                  "brandType"                                      : 2,
                  "executeType"                                    : 1,
                  "auditFlowId"                                    : "CDKG-20230626-056",
                  "status"                                         : 31,
                  "l1OrgName"                                      : "行业运营",
                  "l2OrgName"                                      : "女装",
                  "startTime"                                      : 1680278400000,
                  "endTime"                                        : 1704038399999,
                  "creator"                                        : "cengxiankai",
                  "lowSettleAmtTarget"                             : 1500000000,
                  "midSettleAmtTarget"                             : 2034020000,
                  "highSettleAmtTarget"                            : 2139980000,
                  "lowDisSettleAmtTarget"                          : 5817990000,
                  "midDisSettleAmtTarget"                          : 6323900000,
                  "highDisSettleAmtTarget"                         : 6829820000,
                  "lowAdCostTarget"                                : null,
                  "midAdCostTarget"                                : null,
                  "highAdCostTarget"                               : null,
                  "lowAdCostRebate"                                : null,
                  "midAdCostRebate"                                : null,
                  "highAdCostRebate"                               : null,
                  "lowSettleAmtRebate"                             : "*结算TR*25%",
                  "midSettleAmtRebate"                             : "*结算TR*60%",
                  "highSettleAmtRebate"                            : "*结算TR*82.5%",
                  "lowDisSettleAmtRebate"                          : "*被分销结算TR*10%",
                  "midDisSettleAmtRebate"                          : "*被分销结算TR*15%",
                  "highDisSettleAmtRebate"                         : "*被分销结算TR*20%",
                  "securityDeposit"                                : 3659988,
                  "securityDepositStatus"                          : 0,
                  "frameDays"                                      : 274,
                  "frameCompDays"                                  : 90,
                  "isMainSlr"                                      : 0,
                  "adJkTotalCostCtd"                               : 0.0,
                  "adJfTotalCostCtd"                               : 0.0,
                  "settleGmvCtd"                                   : null,
                  "fromSellerPlatfromSettleAmtCtd"                 : 660713,
                  "disSettleGmvCtd"                                : null,
                  "disFromSellerPlatfromSettleAmtCtd"              : 5669812,
                  "totalCostCtd"                                   : null,
                  "settleTrCtd"                                    : 0.0498208,
                  "disSettleTrCtd"                                 : 0.04992197,
                  "top1BrandPayGmvCtd"                             : null,
                  "payGmvCtd"                                      : null,
                  "isVideoSlr"                                     : 0,
                  "isS12Slr"                                       : 0,
                  "frameDisSettleGmvCtd"                           : null,
                  "adActyDaysLastYear"                             : null,
                  "frameDisFromSellerPlatfromSettleAmtCtd"         : 5669812,
                  "frameDisSettleTrCtd"                            : 0.04992197,
                  "settleGmvLastMonthCtd"                          : null,
                  "fromSellerPlatfromLastMonthCtd"                 : 228140,
                  "disSettleGmvLastMonthCtd"                       : null,
                  "disFromSellerPlatfromSettleAmtLastMonthCtd"     : 1452383,
                  "frameDisSettleGmvLastMonthCtd"                  : null,
                  "frameDisFromSellerPlatfromSettleAmtLastMonthCtd": 1452383,
                  "settleTrLastMonthCtd"                           : 0.04960494,
                  "disSettleTrLastMonthCtd"                        : 0.05003017,
                  "frameDisSettleTrLastMonthCtd"                   : 0.05003017,
                  "isContractRisk"                                 : 0,
                  "pDate"                                          : "20230630"]]
def quarter = 2
def businessStats = []
if (contractInfo == null) {
    throw new IllegalArgumentException("contractInfo is null")
}
//===========合同基本信息======================
//框类型
def agreementType = contractInfo.agreementType
//是否品牌
def isBrandSeller = contractInfo.brandType == 1
//是否是合框
def combineAgreementType = agreementType in ['COMBINE_CUSTOM_SELLER', 'COMBINE_CUSTOM_SELLER_GOODS', 'COMBINE_NORMAL_SELLER', 'COMBINE_NORMAL_SELLER_GOODS']
//是否单边框
def singleAgreementType = agreementType in ['SINGLE_SELLER', 'SINGLE_SELLER_GOODS', 'SINGLE_SELLER_ONLY_GMV', 'SINGLE_SELLER_GOODS_ONLY_GMV']
//是否主播框
def sellerAgreementType = agreementType in ['SINGLE_SELLER', 'COMBINE_CUSTOM_SELLER', 'COMBINE_NORMAL_SELLER', 'SINGLE_SELLER_ONLY_GMV', 'INVITE_SELLER']
//是否主播+货主框
def sellerGoodsAgreementType = agreementType in ['SINGLE_SELLER_GOODS', 'COMBINE_CUSTOM_SELLER_GOODS', 'COMBINE_NORMAL_SELLER_GOODS', 'SINGLE_SELLER_GOODS_ONLY_GMV', 'INVITE_SELLER_GOODS']
//电商单边框/主播/仅GMV
def onlyGmvSellerAgreementType = agreementType in ['SINGLE_SELLER_ONLY_GMV']
//电商单边框/主播+货主/仅GMV
def onlyGmvSellerGoodsAgreementType = agreementType in ['SINGLE_SELLER_GOODS_ONLY_GMV']
//仅GMV框
def onlyGmvAgreementType = agreementType in ['SINGLE_SELLER_ONLY_GMV', 'SINGLE_SELLER_GOODS_ONLY_GMV']
//招商框
def inviteAgreementType = agreementType in ['INVITE_SELLER', 'INVITE_SELLER_GOODS']
//招商框/主播框
def inviteSellerAgreementType = agreementType in ['INVITE_SELLER']
//招商框/主播+货主框
def inviteSellerGoodsAgreementType = agreementType in ['INVITE_SELLER_GOODS']
//是否季框
def quarterAgreement = DateUtil.isSameDay(DateUtil.date(contractInfo.startTime), DateUtil.parseDate("2023-04-01"))


def Q = quarter as int
//计费结果
def result = [settlementGmvRebate: 0, disSettlementGmvRebate: 0, businessRebate: 0]
def attach = [:]
result.attach = attach
result.ecBill = true
result.ecBillRebate = 0
result.businessBill = combineAgreementType
result.businessBillRebate = 0
attach << [settlementFlag: true]
if (CollUtil.isEmpty(baseStats)) {
    attach << [settlementFlag: false, unSettlementCode: 'baseStatsEmpty']
    return result
}

//=============合框 & 品牌商家需要判断：  所有店铺的 top1品牌GMV ÷ 所有店铺总GMV ≥ 60%
//=============合框 & 短视频商家 & 非S12品牌商家:  商业化返点 = max (1.5%，实际档位对应的返点比例-1.5%）
//是否短视频商家
def shortVideoSeller = CollUtil.isNotEmpty(businessStats) && businessStats[0].isShortVideoSeller == 1
def s12BrandSeller = false
//商业化返点特殊处理
if (isBrandSeller && combineAgreementType) {
    def top1Brand
    def totalBrandGmv = 0
    if (CollUtil.isNotEmpty(brandStats)) {
        brandStats.findAll { it.ksBrandId == null }.each { it.ksBrandId = -1 }
        def groupBrand = brandStats.groupBy { it.ksBrandId }
                .collect { k, v -> [ksBrandId: k, outerBrandLevel: v.get(0).outerBrandLevel, selfSettleGmvCtd: v.sum { it.selfSettleGmvCtd }] }
                .sort { it.selfSettleGmvCtd }.reverse()
        top1Brand = groupBrand.find { it.ksBrandId > 0 }
        totalBrandGmv = brandStats.sum { it.selfSettleGmvCtd }
    }
    //是否S12Seller商家
    s12BrandSeller = top1Brand != null && top1Brand.outerBrandLevel in ['S1', 'S2']
    //top1品牌gmv
    def top1BrandGmv = top1Brand != null ? top1Brand.selfSettleGmvCtd : 0
    //top1品牌gmv占比
    def top1BrandGmvRate = totalBrandGmv == 0 ? 0d : NumberUtil.div(top1BrandGmv, totalBrandGmv, 4, RoundingMode.HALF_UP)
    attach << [top1BrandGmv: top1BrandGmv, totalBrandGmv: totalBrandGmv, top1BrandGmvRate: NumberUtil.toStr(top1BrandGmvRate), shortVideoSeller: shortVideoSeller, s12BrandSeller: s12BrandSeller]
    if (top1BrandGmvRate < 0.6d) {
        attach << [settlementFlag: false, unSettlementCode: 'brandGmvNotReach']
        result.attach = attach
        return result
    }
}

//=============年框基础指标计算========================================
//结算GMV 合同生效至今累计值
long settlementGmv = baseStats.sum { it.settleGmvCtd }
//分销结算GMV 合同生效至今累计值
long disSettlementGmv = sellerGoodsAgreementType ? baseStats.sum { it.frameDisSettleGmvCtd } : baseStats.sum { it.disSettleGmvCtd }
//商业化计框口径 合同生效至今累计值
long jkBusinessTotalCost = NumberUtil.round(baseStats.sum { it.adJkTotalCostCtd }, 0, RoundingMode.HALF_UP).longValue()
//商业化计返口径 合同生效至今累计值
long jfBusinessTotalCost = NumberUtil.round(baseStats.sum { it.adJfTotalCostCtd }, 0, RoundingMode.HALF_UP).longValue()
//电商口径商业化消耗 合同生效至今累计值
long dsBusinessTotalCost = NumberUtil.round(baseStats.sum { it.totalCostCtd }, 0, RoundingMode.HALF_UP).longValue()
//结算TR
def divF = { long a, long b ->
    if (b == 0L) {
        return 0d
    }
    return NumberUtil.div(a, b, 4, RoundingMode.HALF_UP).doubleValue()
}
def settleTrLastMonth = {
    if (Q in [1, 2, 3]) {
        return divF(baseStats.sum { it.fromSellerPlatfromLastMonthCtd }, baseStats.sum { it.settleGmvLastMonthCtd })
    } else if (Q == 4) {
        return divF(baseStats.sum { it.fromSellerPlatfromSettleAmtCtd }, baseStats.sum { it.settleGmvCtd })
    }
}
//分销结算TR
def disSettleTrLastMonth = {
    if (Q in [1, 2, 3]) {
        //主播+货主框
        if (sellerGoodsAgreementType) {
            return divF(baseStats.sum { it.frameDisFromSellerPlatfromSettleAmtLastMonthCtd }, baseStats.sum { it.frameDisSettleGmvLastMonthCtd })
        } else {
            return divF(baseStats.sum { it.disFromSellerPlatfromSettleAmtLastMonthCtd }, baseStats.sum { it.disSettleGmvLastMonthCtd })
        }
    } else if (Q == 4) {
        //主播+货主框
        if (sellerGoodsAgreementType) {
            return divF(baseStats.sum { it.frameDisFromSellerPlatfromSettleAmtCtd }, baseStats.sum { it.frameDisSettleGmvCtd })
        } else {
            return divF(baseStats.sum { it.disFromSellerPlatfromSettleAmtCtd }, baseStats.sum { it.disSettleGmvCtd })
        }
    }
}
//22年运营天数
def settlementDaysLastYearF = {
    if (quarterAgreement) {
        return it.rslrActyDaysLastYear
    }
    return Math.min(365, it.rslrActyDaysLastYear as long + 65)
}
//通道费
def gmvTrRate = quarterAgreement ? 0.009d : 0.006d

//目标完成档位
def targetInfo = contractInfo.targetInfo.gearInfoList
def sendType = contractInfo.sendType
//框架总天数=合同结束日期-合同生效日期+1
def agreementTotalDays = DateUtil.betweenDay(DateUtil.date(contractInfo.startTime), DateUtil.date(contractInfo.endTime), true) + 1
//已执行天数=结算结束日期-合同开始时间
def agreementPerformDays = DateUtil.betweenDay(DateUtil.date(contractInfo.startTime), DateUtil.parse(settlementEnd), true) + 1
def completionTarget = targetInfo.get(sendType - 1)
//目标完成度
def quarterRateMap = quarterAgreement ? [1: 0.86d, 2: 0.946d, 3: 0.98d, 4: 1d] : [1: 0.86d, 2: 0.883d, 3: 0.905d, 4: 1d]
//累计时间进度 累计框架已执行天数/框架总天数
def agreementPerformProcess = NumberUtil.round(agreementPerformDays / agreementTotalDays, 4, RoundingMode.HALF_UP).doubleValue()
//完成率要求 = 累计时间进度 * 对应季度百分比
def completionRateTarget = NumberUtil.round(agreementPerformProcess * (quarterRateMap.get(Q)), 4, RoundingMode.HALF_UP).doubleValue()
attach += [settlementGmv       : settlementGmv, disSettlementGmv: disSettlementGmv, jfBusinessTotalCost: jfBusinessTotalCost,
           jkBusinessTotalCost : jkBusinessTotalCost, dsBusinessTotalCost: dsBusinessTotalCost, agreementTotalDays: agreementTotalDays,
           agreementPerformDays: agreementPerformDays, agreementPerformProcess: NumberUtil.toStr(agreementPerformProcess), completionRateTarget: NumberUtil.toStr(completionRateTarget)]

//=============Q1~ Q3 当目标完成度>=完成率要求时，进行奖励结算=========
if (Q in [1, 2, 3]) {
    //计算GMV的完成度
    def settlementGmvCompletionRate = divF(settlementGmv, completionTarget.settleGmvTarget)
    def disSettlementGmvCompletionRate = divF(disSettlementGmv, completionTarget.beFxSettleGmvTarget)
    def businessTotalCost = combineAgreementType ? jkBusinessTotalCost : dsBusinessTotalCost
    def businessTotalCostCompletionRate = divF(businessTotalCost, completionTarget.businessCostTarget)
    def completionRate
    //主播框
    if (sellerAgreementType) {
        //单边框/主播/仅GMV
        if (onlyGmvSellerAgreementType) {
            completionRate = settlementGmvCompletionRate
            //招商框/主播框
        } else if (inviteSellerAgreementType) {
            if (completionTarget.businessCostTarget > 0L) {
                completionRate = NumberUtil.min(settlementGmvCompletionRate, businessTotalCostCompletionRate)
            } else {
                completionRate = NumberUtil.min(settlementGmvCompletionRate)
            }
        } else {
            completionRate = NumberUtil.min(settlementGmvCompletionRate, businessTotalCostCompletionRate)
        }
        //主播+货主框
    } else if (sellerGoodsAgreementType) {
        //单边框/主播+货主/仅GMV
        if (onlyGmvSellerGoodsAgreementType) {
            completionRate = NumberUtil.min(settlementGmvCompletionRate, disSettlementGmvCompletionRate)
            //招商框/主播+货主框
        } else if (inviteSellerGoodsAgreementType) {
            if (completionTarget.businessCostTarget > 0L) {
                completionRate = NumberUtil.min(settlementGmvCompletionRate, disSettlementGmvCompletionRate, businessTotalCostCompletionRate)
            } else {
                completionRate = NumberUtil.min(settlementGmvCompletionRate, disSettlementGmvCompletionRate)
            }
        } else {
            completionRate = NumberUtil.min(settlementGmvCompletionRate, disSettlementGmvCompletionRate, businessTotalCostCompletionRate)
        }
    }

    attach << [settlementGmvCompletionRate: NumberUtil.toStr(settlementGmvCompletionRate), disSettlementGmvCompletionRate: NumberUtil.toStr(disSettlementGmvCompletionRate), businessTotalCostCompletionRate: NumberUtil.toStr(businessTotalCostCompletionRate), completionRate: NumberUtil.toStr(completionRate)]
    //不满足完成率，不结算
    if (completionRate <= completionRateTarget) {
        attach << [settlementFlag: false, unSettlementCode: 'completionRateNotReach']
        result.attach = attach
        return result
    }
}

//====================Q4，需要按照实际档位进行结算================================
//最终结算档位
def rebateLevel = sendType
if (Q == 4) {
    def targetLevel = { value, Closure cls ->
        if (value < cls(targetInfo.get(0))) {
            return -1
        } else if (value >= cls(targetInfo.get(0)) && value < cls(targetInfo.get(1))) {
            return 1
        } else if (value >= cls(targetInfo.get(1)) && value < cls(targetInfo.get(2))) {
            return 2
        } else {
            return 3
        }
    }
    def settlementGmvLevel = targetLevel(settlementGmv, { it.settleGmvTarget })
    def businessTotalCost = combineAgreementType ? jkBusinessTotalCost : dsBusinessTotalCost
    def businessGmvLevel = targetLevel(businessTotalCost, { it.businessCostTarget })

    if (sellerAgreementType) {
        if (onlyGmvSellerAgreementType) {
            rebateLevel = settlementGmvLevel
        } else if (inviteSellerAgreementType) {
            if (completionTarget.businessCostTarget > 0L) {
                rebateLevel = NumberUtil.min(settlementGmvLevel, businessGmvLevel)
            } else {
                rebateLevel = settlementGmvLevel
            }
        } else {
            rebateLevel = NumberUtil.min(settlementGmvLevel, businessGmvLevel)
        }
    } else {
        def disSettlementGmvLevel = targetLevel(disSettlementGmv, { it.beFxSettleGmvTarget })
        if (onlyGmvSellerGoodsAgreementType) {
            rebateLevel = NumberUtil.min(settlementGmvLevel, disSettlementGmvLevel)
        } else if (inviteSellerGoodsAgreementType) {
            if (completionTarget.businessCostTarget > 0L) {
                rebateLevel = NumberUtil.min(settlementGmvLevel, disSettlementGmvLevel, businessGmvLevel)
            } else {
                rebateLevel = NumberUtil.min(settlementGmvLevel, disSettlementGmvLevel)
            }
        } else {
            rebateLevel = NumberUtil.min(settlementGmvLevel, disSettlementGmvLevel, businessGmvLevel)
        }
    }
    //完成率不达标
    if (rebateLevel == -1) {
        attach << [settlementFlag: false, unSettlementCode: 'completionRateNotReach']
        result.attach = attach
        return result
    }
}


//=========================进行最终的返点金额计算==================================
//==============================================================================
def target = targetInfo.get(rebateLevel - 1)
attach += [rebateLevel: rebateLevel]
//========结算GMV返点==================================
//合框 && 低目标没有结算GMV返点
if (rebateLevel > 1 || singleAgreementType || inviteAgreementType) {
    //增量结算GMV
    long incSettlementGmv = baseStats.sum {
        //22年运营天数
        def settlementDaysLastYear = settlementDaysLastYearF(it)
        //22年日均结算GMV
        def settlementGmvAvgDaily = settlementDaysLastYear == 0 ? 0L : NumberUtil.div(it.settleGmvLastYear, settlementDaysLastYear, 0, RoundingMode.HALF_UP).longValue()
        //增量结算GMV = 实际结算GMV-22年日均结算GMV * 年框已执行天数
        return it.settleGmvCtd - settlementGmvAvgDaily * agreementPerformDays
    }
    incSettlementGmv = incSettlementGmv > 0 ? incSettlementGmv : 0L
    def settlementTr = settleTrLastMonth()
    if (settlementTr < gmvTrRate) {
        result.settlementGmvRebate = 0
    } else {
        result.settlementGmvRebate = NumberUtil.round(incSettlementGmv * (settlementTr - gmvTrRate) * (target.gmvTrRebateWf / 10000), 0, RoundingMode.HALF_UP).longValue()
    }
    attach << [incSettlementGmv: incSettlementGmv, settlementTr: NumberUtil.toStr(settlementTr)]
}

//========分销结算GMV返点==================================
//只有主播+货主框，才会有分销结算GMV
if (sellerGoodsAgreementType) {
    long incDisSettlementGmv = baseStats.sum {
        //如果存在风控关联关系，则被被分销gmv一律置0
        if (it.isContractRisk == 1) {
            return 0L
        }
        //22年日均分销结算GMV
        def disSettlementGmvAvgDaily = it.actyDaysLastYear == 0 ? 0L : NumberUtil.div(it.disSettleGmvLastYear, it.actyDaysLastYear, 0, RoundingMode.HALF_UP).longValue()
        //增量被分销结算GMV = 实际分销结算GMV-22年日均分销结算GMV * 年框已执行天数
        return (sellerGoodsAgreementType ? it.frameDisSettleGmvCtd : it.disSettleGmvCtd) - disSettlementGmvAvgDaily * agreementPerformDays
    }
    incDisSettlementGmv = incDisSettlementGmv > 0L ? incDisSettlementGmv : 0L
    def disSettlementTr = disSettleTrLastMonth()
    if (disSettlementTr < gmvTrRate) {
        result.disSettlementGmvRebate = 0L
    } else {
        result.disSettlementGmvRebate = NumberUtil.round(incDisSettlementGmv * (disSettlementTr - gmvTrRate) * (target.beFxGmvTrRebateWf / 10000), 0, RoundingMode.HALF_UP).longValue()
    }
    attach << [incDisSettlementGmv: incDisSettlementGmv, disSettlementTr: NumberUtil.toStr(disSettlementTr)]
}

//========商业化消耗GMV返点==================================
//仅GMV框，没有商业化返点
if (onlyGmvAgreementType) {


    //合框 or 招商框，商业化消耗返点金额 = 23年全量消耗 * 返点比例
} else if (combineAgreementType || (inviteAgreementType && completionTarget.businessCostTarget > 0)) {
    def costRebate = NumberUtil.round(target.businessCostRebateWf / 10000, 4, RoundingMode.HALF_UP).doubleValue()
    //短视频商家 & 非S12品牌商家，需要商业化返点特殊处理
    if (combineAgreementType && shortVideoSeller && !s12BrandSeller) {
        costRebate = NumberUtil.round(NumberUtil.max(0.015d, costRebate - 0.015d), 4, RoundingMode.HALF_UP).doubleValue()
    }
    def businessTotalCost = combineAgreementType ? jfBusinessTotalCost : dsBusinessTotalCost
    result.businessRebate = NumberUtil.round(businessTotalCost * costRebate, 0, RoundingMode.HALF_UP).longValue()

    //电商单边框 商业化消耗返点金额 = min (增量消耗 * xx%，23年全量消耗*返点比例）
} else if (singleAgreementType) {
    long incBusinessTotalCost = baseStats.sum {
        //22年日均商业化消耗
        def businessTotalCostAvgDaily = it.adActyDaysLastYear == 0 ? 0L : NumberUtil.div(it.totalCostLastYear, it.adActyDaysLastYear, 0, RoundingMode.HALF_UP).longValue()
        //增量商业化消耗
        return it.totalCostCtd - businessTotalCostAvgDaily * agreementPerformDays
    }
    incBusinessTotalCost = incBusinessTotalCost > 0L ? incBusinessTotalCost : 0L
    def businessRebate = NumberUtil.round(incBusinessTotalCost * (target.businessCostYearlyRebateWf / 10000), 0, RoundingMode.HALF_UP).longValue()
    //商业化消耗中 = min (增量消耗*xx%，23年全量消耗*返点比例）
    result.businessRebate = Math.min(businessRebate, NumberUtil.round(dsBusinessTotalCost * (target.businessCostRebateWf / 10000), 0, RoundingMode.HALF_UP).longValue())
    attach << [incBusinessTotalCost: incBusinessTotalCost]
}
result.totalRebate = result.settlementGmvRebate + result.disSettlementGmvRebate + result.businessRebate
if (combineAgreementType) {
    result.ecBillRebate = result.settlementGmvRebate + result.disSettlementGmvRebate
} else {
    result.ecBillRebate = result.settlementGmvRebate + result.disSettlementGmvRebate + result.businessRebate
}
if (result.businessBill) {
    result.businessBillRebate = result.businessRebate
}
result.attach = attach
return result